//
//  TableShowWKWebCell.swift
//  CooperSwift
//
//  Created by 计海峰 on 2021/12/9.
//

import UIKit
import WebKit
import RxSwift
import Foundation

// MARK: - cell
public class TableShowWKWebCell: UITableViewCell {
    
    let stepBag = DisposeBag()
    
    /// wkwebview
    lazy var webview: WKWebView = {
        var config = WKWebViewConfiguration.init() // wkwebview 配置对象
        config.preferences = WKPreferences.init() // 偏好配置
        config.preferences.minimumFontSize = 12 // 默认最小字体
        // 不通过用户交互，是否可以打开窗口
        config.preferences.javaScriptCanOpenWindowsAutomatically = false
        var noneSelectScript: WKUserScript?
        
        var javascript = ""
        javascript += "document.documentElement.style.webkitTouchCallout='none';" // 禁止长按
        javascript += "document.documentElement.style.webkitUserSelect='none';" // 禁止选择
        noneSelectScript = WKUserScript.init(source: javascript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)

        var webview = WKWebView.init(frame: UIScreen.main.bounds, configuration: config)
        if (noneSelectScript != nil) {
            webview.configuration.userContentController.addUserScript(noneSelectScript!)
        }
//        webview.uiDelegate = self
//        webview.navigationDelegate = self
//        webview.scrollView.delegate = self
        webview.scrollView.isScrollEnabled = false
        
        contentView.addSubview(webview)
        webview.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
        /// 监听 contentSize
        webview.scrollView.rx.observe(CGSize.self, "contentSize")
        .filter { return $0 != nil }
        .distinctUntilChanged()
        .filter {  [weak self] size in
            guard let vm = self else { return false }
            guard let cellModel = vm.info as? TableShowWKWebCellModel else { return false }
            return abs(cellModel.cellHeight - size!.height) > 5
        }
        .debounce(.milliseconds(100), scheduler: MainScheduler.instance)
        .subscribe(onNext: { [weak self] size in
            guard let vm = self else { return }
            guard let cellModel = vm.info as? TableShowWKWebCellModel else { return }
            debugPrint(size!)
            cellModel.cellHeight = size!.height
            cellModel.postTableService(.updatesTable)
        }).disposed(by: stepBag)
        
        return webview
    }()
    

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override func bindTo(_ info: Any?) {
        self.info = info
        self.layoutViews()
    }
    
    public override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    deinit {
        debugPrint("💔 TableShowWKWebCell deinit")
    }
    
}
